View Javadoc

1   package uk.ac.roe.antigen.dialogs;
2   
3   import java.awt.BorderLayout;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import java.io.BufferedOutputStream;
7   import java.io.IOException;
8   import java.io.InputStreamReader;
9   import java.io.OutputStream;
10  import java.io.PrintStream;
11  import java.nio.ByteBuffer;
12  import java.nio.charset.Charset;
13  import java.util.logging.Logger;
14  
15  import javax.swing.BorderFactory;
16  import javax.swing.ImageIcon;
17  import javax.swing.JButton;
18  import javax.swing.JPanel;
19  import javax.swing.JProgressBar;
20  import javax.swing.JScrollPane;
21  import javax.swing.JTextArea;
22  import javax.swing.border.BevelBorder;
23  import javax.swing.border.LineBorder;
24  
25  import org.apache.tools.ant.BuildEvent;
26  import org.apache.tools.ant.BuildLogger;
27  import org.apache.tools.ant.DefaultLogger;
28  
29  import sun.rmi.runtime.Log;
30  import uk.ac.roe.antigen.utils.Config;
31  
32  
33  
34  
35  
36  public class BuildLoggerFrame extends AntigenFrame {
37      /***
38       * Logger for this class
39       */
40      private static final Logger logger = Logger.getLogger(BuildLoggerFrame.class.getName());
41  
42  	private JTextArea outputArea;
43  
44  	private JPanel buttons;
45  
46  	private JScrollPane jScrollPane1;
47  	private JProgressBar jProgressBar1;
48  
49  	private JButton okBtn;
50  
51  	/***
52  	 * Auto-generated main method to display this JDialog
53  	 */
54  	public static void main(String[] args) {
55          Config.load("/config.properties");
56  		BuildLoggerFrame inst = new BuildLoggerFrame();
57  		inst.setVisible(true);
58  		PrintStream test = new PrintStream(inst.new LocalOutputStream());
59  		test.println("Hello World");
60  	}
61  
62      public class InstallerAntLogger extends DefaultLogger {
63          /***
64           * Logger for this class
65           */
66          private final Logger logger = Logger.getLogger(InstallerAntLogger.class.getName());
67  
68  
69          /* (non-Javadoc)
70           * @see org.apache.tools.ant.BuildListener#taskFinished(org.apache.tools.ant.BuildEvent)
71           */
72          public void taskFinished(BuildEvent arg0) {
73              super.taskFinished(arg0);
74            //  System.out.println("Task finished");
75              incrementProgress();
76          }
77      }
78  
79      private PrintStream printStream = new PrintStream(new BufferedOutputStream(new LocalOutputStream()));
80      
81  	public BuildLoggerFrame() {
82  		super();
83          initGUI2();
84  		antLogger = new InstallerAntLogger();
85  		
86  		antLogger.setOutputPrintStream(getPrintStream());
87          System.setOut(getPrintStream());
88  	}
89  
90      /***
91       * @return Returns the printStream to access the build pane.
92       */
93      PrintStream getPrintStream() {
94          return printStream;
95      }
96  
97      private class LocalOutputStream extends OutputStream {
98          /***
99           * The number of chars in the window will be between [MAX_CHARS_IN_WINDOW/2,MAX_CHARS_IN_WINDOW]
100          */
101         private static final int MAX_CHARS_IN_WINDOW = 40000;
102         /***
103          * Kludge to get default char set
104          */
105         private Charset defaultCharSet = Charset.forName(new InputStreamReader(System.in).getEncoding());
106         
107         public void write(byte[] b, int off, int len) {
108             ByteBuffer buffer = ByteBuffer.wrap(b, off, len);
109 
110             String newText = defaultCharSet.decode(buffer).toString();
111             outputArea.append(newText);
112             
113             String allText = outputArea.getText();
114             int allTextLength = allText.length();
115             if (allTextLength>MAX_CHARS_IN_WINDOW) {
116                 String cutText = allText.substring(allTextLength-MAX_CHARS_IN_WINDOW/2);
117                 outputArea.setText(cutText);
118                 logger.fine(""+cutText.length());
119                 allTextLength=MAX_CHARS_IN_WINDOW/2;
120             }
121             
122             outputArea.setCaretPosition(allTextLength);
123         }
124 
125 		/*
126 		 * (non-Javadoc)
127 		 * 
128 		 * @see java.io.OutputStream#write(int)
129 		 */
130 		public void write(int arg0) throws IOException {
131 			char[] chars = { (char) arg0 };
132 			String newText = new String(chars);
133 			outputArea.append(newText);
134 			outputArea.setCaretPosition(outputArea.getText().length());
135 		}
136 
137 	}
138 
139 	/***
140 	 * 
141 	 */
142 	private void initGUI2() {
143 		{
144 			jScrollPane1 = new JScrollPane();
145 			workPanel.add(jScrollPane1, BorderLayout.CENTER);
146 			jScrollPane1.setAutoscrolls(true);
147 			jScrollPane1
148 				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
149 			{
150 				outputArea = new JTextArea();
151 				jScrollPane1.setViewportView(outputArea);
152 				//outputArea.setText("Build log:");
153 				outputArea.setBorder(new LineBorder(new java.awt.Color(
154 					0,
155 					255,
156 					255), 1, true));
157 				outputArea.setBackground(new java.awt.Color(0, 0, 0));
158 				outputArea
159 					.setForeground(new java.awt.Color(0, 255, 255));
160 				outputArea.setLineWrap(true);
161 				outputArea.setEditable(false);
162 				outputArea.setFocusable(false);
163 			}
164 		}
165 		{
166 			jProgressBar1 = new JProgressBar();
167 			workPanel.add(jProgressBar1, BorderLayout.SOUTH);
168 			jProgressBar1.setSize(400, 14);
169 			jProgressBar1.setBorder(BorderFactory.createBevelBorder(
170 				BevelBorder.LOWERED,
171 				null,
172 				null,
173 				null,
174 				null));
175 		}
176         {
177             buttons = new JPanel();
178             southPanel.add(buttons, BorderLayout.SOUTH);
179             buttons.setOpaque(false);
180             {
181                 okBtn = new JButton();
182                 buttons.add(okBtn);
183                 okBtn.setText("Done");
184                 okBtn.setEnabled(false);
185                 okBtn.setIcon(new ImageIcon(getClass().getClassLoader()
186                     .getResource("img/exit.png")));
187                 okBtn.addActionListener(new ActionListener() {
188                     public void actionPerformed(ActionEvent evt) {
189                         shutDown();
190                     }
191                 });
192             }
193         }
194 	}
195 
196 	private DefaultLogger antLogger;
197 
198 	private int numberOfTasks;
199 
200 	public BuildLogger getBuildLogger() {
201 		return antLogger;
202 	}
203 
204 	public void incrementProgress() {
205        jProgressBar1.setMaximum(numberOfTasks-1);
206        jProgressBar1.setValue(currentTaskNumber++);
207 	logger.fine("incrementProgress() - current task " + currentTaskNumber
208 			+ " / " + numberOfTasks);
209 	}
210 	private int currentTaskNumber = 0;
211 	/***
212 	 * @param numberOfTasks
213 	 */
214 	public void setNumberOfTasks(int numberOfTasks) {
215 		this.numberOfTasks = numberOfTasks;
216 	}
217 
218 
219 	public void setFinished() {
220         jProgressBar1.setValue(jProgressBar1.getMaximum());
221 		okBtn.setEnabled(true);
222 	}
223 
224 	/***
225 	 *  Time to clean up and closedown 
226 	 */
227 	public void shutDown() {
228 	    logger.info("BuildLogger window closing");
229         dispose();
230 	}
231 
232 }